﻿namespace Digitalmes.Domain.Mes;

/// <summary>
/// 生产工单
/// </summary>
[SugarTable("prod_wo", "生产工单表")]
[SugarIndex("index_prod_wo_no", nameof(Wo), OrderByType.Asc)]
public class ProdWo : EntityBase
{
    /// <summary>
    /// 工单单号
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "工单号", Length = 32)]
    public string? Wo { get; set; }

    /// <summary>
    /// 产品 Id
    /// </summary>
    [SugarColumn(ColumnDescription = "产品Id")]
    public long ProductId { get; set; }

    /// <summary>
    /// 产品（成品）信息
    /// </summary>
    [Navigate(NavigateType.OneToOne, nameof(ProductId))]
    public MdItem? Product { get; set; }

    /// <summary>
    /// 产品代码
    /// </summary>
    [NotNull]
    [SugarColumn(ColumnDescription = "产品代码", Length = 64)]
    public string? ProductCode { get; set; }

    /// <summary>
    /// 总数量
    /// </summary>
    [SugarColumn(ColumnDescription = "总数量")]
    public int TotalQty { get; set; }

    /// <summary>
    /// 计划开始时间
    /// </summary>
    [SugarColumn(ColumnDescription = "计划开始时间")]
    public DateTime PlanStartTime { get; set; }

    /// <summary>
    /// 计划结束时间
    /// </summary>
    [SugarColumn(ColumnDescription = "计划结束时间")]
    public DateTime PlanEndTime { get; set; }

    /// <summary>
    /// 实际开始时间
    /// </summary>
    [SugarColumn(ColumnDescription = "实际开始时间")]
    public DateTime? ActualStartTime { get; set; }

    /// <summary>
    /// 实际完工时间
    /// </summary>
    [SugarColumn(ColumnDescription = "实际完工时间")]
    public DateTime? ActualEndTime { get; set; }

    /// <summary>
    /// 工单来源（本地创建，Erp下发）
    /// </summary>
    [SugarColumn(ColumnDescription = "单据状态")]
    public WoSourceEnum Source { get; set; }

    /// <summary>
    /// 批次号
    /// </summary>
    [SugarColumn(ColumnDescription = "批次号")]
    public string? BatchNo { get; set; }

    /// <summary>
    /// 单据状态
    /// </summary>
    [SugarColumn(ColumnDescription = "单据状态")]
    public WoStatusEnum Status { get; set; }

    /// <summary>
    /// 上一次单据切换状态
    /// </summary>
    /// <remarks>当工单从"暂停"状态恢复时，可以回到上一次切换"暂停"的状态。</remarks>
    public WoStatusEnum LastStatus { get; set; }

    /// <summary>
    /// 上线数量。
    /// </summary>
    /// <remarks>产品已上线开始生产的数量，可计算此工单 “在制品” 数量（上线数 - 完工数）。</remarks>
    [SugarColumn(ColumnDescription = "上线数量")]
    public int OnlineQty { get; set; }

    /// <summary>
    /// 完工数量
    /// </summary>
    /// <remarks>产品完工下线的数量，其中 完工数 = 合格数 + 不合格数 + 报废数</remarks>
    [SugarColumn(ColumnDescription = "完工数量")]
    public int CompletedQty { get; set; }

    /// <summary>
    /// 合格数量
    /// </summary>
    /// <remarks>产品完工下线且合格的数量。</remarks>
    [SugarColumn(ColumnDescription = "合格数量")]
    public int QualifiedQty { get; set; }

    /// <summary>
    /// 不合格数
    /// </summary>
    /// <remarks>产品完工下线且不合格的数量，经过返修后可更改为合格数量。</remarks>
    [SugarColumn(ColumnDescription = "不合格数量")]
    public int UnqualifiedQty { get; set; }

    /// <summary>
    /// 报废数量
    /// </summary>
    [SugarColumn(ColumnDescription = "报废数量")]
    public int ScrapQty { get; set; }

    /// <summary>
    /// 详细集合
    /// </summary>
    [Navigate(NavigateType.OneToMany, nameof(ProdWoDetail.WoId))]
    public List<ProdWoDetail>? Details { get; set; }

    /// <summary>
    /// 设置工单状态
    /// </summary>
    /// <param name="status"></param>
    public void SetStatus(WoStatusEnum status)
    {
        LastStatus = Status;
        Status = status;
    }

    /// <summary>
    /// 恢复至上一状态
    /// </summary>
    public void RecoverStatus()
    {
        (LastStatus, Status) = (Status, LastStatus);
    }
}
